package com.mc.dao.Impl;

import com.mc.bean.MatchState;
import com.mc.dao.MatchStateDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 赛事状态实体类数据访问接口实现类
 * @author wangcf
 * @date 2023/2/13 15:24
 * @version 1.0
 */
public class MatchStateDaoImpl extends BaseDao implements MatchStateDao {

    @Override
    public List<MatchState> findAll() {
        List<MatchState> matchStateList=new ArrayList<>();
        try {
            regist();
            connection();
            getPst("select * from matchstate");
            getRs();
            while (rs.next()){
                MatchState matchState=new MatchState(rs.getInt(1),rs.getString(2));
                matchStateList.add(matchState);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchStateList;
    }

    @Override
    public MatchState findByMSid(int msid) {
        MatchState matchState=null;
        try {
            regist();
            connection();
            getPst("select *from matchstate where msid=?");
            pstmt.setInt(1,msid);
            getRs();
            while (rs.next()){
                matchState=new MatchState(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return matchState;
    }

    @Override
    public int add(MatchState matchState) {
        int i=0;
        try {
            regist();
            connection();
            getPst("insert into matchstate (msname) values (?)");
            pstmt.setString(1,matchState.getMsname());
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int delete(int msid) {
        int i=0;
        try {
            regist();
            connection();
            getPst("delete from matchstate where msid=?");
            pstmt.setInt(1,msid);
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int update(MatchState matchState) {
        int i=0;
        try {
            regist();
            connection();
            getPst("update matchstate set msname=? where msid=?");
            pstmt.setString(1,matchState.getMsname());
            pstmt.setInt(2,matchState.getMsid());
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }
}
